package com.agd.quick.states;
//C

import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;

import android.view.KeyEvent;
import lib.elementgame.GameSprite;
import lib.elementgame.GameText;
import lib.engine.GameEngine;
import lib.engine.GameState;

public class StateCredit extends GameState {

	private GameSprite bg;
	private GameText[] credit = new GameText[20];
	private Rectangle bg_text;
// IFL_THx.bmp
	private boolean keluar;
	public StateCredit(GameEngine engine) {
		super(engine);
		
	}	
	
	@Override
	public void initComponent() {
		bg = new GameSprite(CR_SPRITE_BACKGROUND, engine);
		for (int i = 0; i < credit.length; i++) {
			if (i % 2 == 0) {
				credit[i] = new GameText("Credit text",150, engine.getFont(FONT_CREDIT_TIMG), engine);
			}
			
			else
			{
				credit[i] = new GameText("Credit text",150, engine.getFont(FONT_CIRCULAT_30_WHITE), engine);
			}
		}

		bg_text = new Rectangle(305, 0, 500, 480, engine.getVertexBufferObjectManager());	
	}

	@Override
	protected void init() {
		engine.camera.setCenter(GameEngine.cameraWidth/2, GameEngine.cameraHeight/2);
		keluar = false;
		for (int i = 0; i < credit.length; i++) {
			credit[i].setHorizontalAlign(HorizontalAlign.CENTER);
			
			if (i % 2 == 0) {
				credit[i].setColor(Color.PINK);
			}
		}

		credit[0].setText("Ketua AGD");
		credit[1].setText("" +
				"Eka Pratiwi\n" +
				"");
		credit[2].setText("Produser Manager");
		credit[3].setText("" +
				"Faradilla Ayu Krisnawati Putri\n" +
				"");
		credit[4].setText("Produser");
		credit[5].setText("" +
				"Reza\n" +
				"");
		credit[6].setText("Game Designer");
		credit[7].setText("" +
				"Rizal\n" +				
				"");
		credit[8].setText("2D Artist");
		credit[9].setText("" +
				"Satya\n" +
				"Nizar\n" +
				"Aba\n"+
				"");
		credit[10].setText("Programmer");
		credit[11].setText("" +
				"Vido\n" +
				"Ifl\n" +
				"Ervan Prastyanto\n" +
				"Abu Amar\n" +
				"");
		credit[12].setText("Sound Enginer");
		credit[13].setText("" +
				"Tendy\n" +
				"Fahmi\n" +
				"Wildan\n"+
				"");
		credit[14].setText("Game Tester");
		credit[15].setText("" +
				"Muhammad Sadli Ryan Firdaus\n" +
				"Riyan dwi yulian p\n" +
				"");
		credit[16].setText("Special Thanks To:");
		credit[17].setText("" +
				"Tuhan YME\n" +
				"Jogja Digital Valley\n" +
				"Rr Galuh Acintya Pravintasari\n" +
				"Rian Arif\n" +
				"Muhammad Nur Septio\n" +
				"Ardi Yudanto\n" +
				"Latif Setyawan\n" +
				"Hari Abriyoko\n" +
				"Yasir Muhammad Arif\n" +
				"");
		credit[18].setText("Thanks To");
		credit[19].setText("" +
				"STMIK AMIKOM YOGYAKARTA\n" +
				"AMIKOM GAME DEVELOPER" +
				"");

		bg_text.setColor(Color.BLACK);
		bg_text.setAlpha(0.5f);
		
	}

	@Override
	protected void attach() {
		
		engine.scene.attachChild(bg);
		engine.scene.attachChild(bg_text);
		for (int i = 0; i < credit.length; i++) {
			bg_text.attachChild(credit[i]);
		}
	}
	
	@Override
	protected void detach() {
		
		bg.detachSelf();
		bg_text.detachSelf();
		for (int i = 0; i < credit.length; i++) {
			credit[i].detachSelf();
		}
	}

	@Override
	protected void setPosition() {
		credit[0].setPosition(bg_text.getWidth()/2 - credit[0].getWidth()/2, bg_text.getHeight());
		for (int i = 1; i < credit.length; i++) {
			credit[i].setPosition(bg_text.getWidth()/2 - credit[i].getWidth()/2, credit[i-1].getY() + credit[i-1].getHeight());
		}

	}

	@Override
	protected void registerTouch() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void unregisterTouch() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void onUpdate() {
		if(keluar){
			exitState(MainMenuState);
		}
		for (int i = 0; i < credit.length; i++) {
			credit[i].setY(credit[i].getY() - 2);
		}

		if(credit[credit.length-1].getY() + credit[credit.length-1].getHeight() <= 0) {
			credit[0].setPosition(bg_text.getWidth()/2 - credit[0].getWidth()/2, bg_text.getHeight());
			for (int i = 1; i < credit.length; i++) {
				credit[i].setPosition(bg_text.getWidth()/2 - credit[i].getWidth()/2, credit[i-1].getY() + credit[i-1].getHeight());
			}
		}
	}

	@Override
	protected void onPaused() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onResumed() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			keluar = true;
		}
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY) {
		// TODO Auto-generated method stub
		
		return false;
	}

	@Override
	public void initLoading() {
		// TODO Auto-generated method stub
		
	}

}
